#include "cabrillo_config_dialog.h"
#include "ui_manager.h"
#include <iostream>
#include <fstream>
#include <glib.h>

CabrilloConfigDialog::CabrilloConfigDialog() : dialog(nullptr), config(nullptr) {
    config = CabrilloConfig::getInstance();
}

CabrilloConfigDialog::~CabrilloConfigDialog() {
    if (dialog) {
        gtk_widget_destroy(dialog);
    }
}

bool CabrilloConfigDialog::show(GtkWindow* parent) {
    GtkBuilder* builder = gtk_builder_new();
    
    // Load UI file directly from the UI directory relative to the executable
    char* exec_path = g_file_read_link("/proc/self/exe", NULL);
    char* ui_path = nullptr;
    
    if (exec_path) {
        char* exec_dir = g_path_get_dirname(exec_path);
        ui_path = g_build_filename(exec_dir, "UI", "cabrillo_config_dialog.glade", NULL);
        g_free(exec_dir);
        g_free(exec_path);
    }
    
    GError* error = nullptr;
    if (!ui_path || !gtk_builder_add_from_file(builder, ui_path, &error)) {
        std::cerr << "无法加载cabrillo_config_dialog.glade: " << (error ? error->message : "未知错误") << std::endl;
        if (error) {
            g_error_free(error);
        }
        g_free(ui_path);
        g_object_unref(builder);
        return false;
    }
    
    g_free(ui_path);
    
    // Get dialog object
    dialog = GTK_WIDGET(gtk_builder_get_object(builder, "cabrillo_config_dialog"));
    if (!dialog) {
        std::cerr << "无法获取cabrillo_config_dialog对象" << std::endl;
        g_object_unref(builder);
        return false;
    }
    
    // Set parent window
    if (parent) {
        gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
    }
    
    // Get entry widgets
    entry_callsign = GTK_WIDGET(gtk_builder_get_object(builder, "entry_callsign"));
    entry_contest = GTK_WIDGET(gtk_builder_get_object(builder, "entry_contest"));
    entry_location = GTK_WIDGET(gtk_builder_get_object(builder, "entry_location"));
    entry_start_of_log = GTK_WIDGET(gtk_builder_get_object(builder, "entry_start_of_log"));
    entry_category_operator = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_operator"));
    entry_category_band = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_band"));
    entry_category_power = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_power"));
    entry_category_assisted = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_assisted"));
    entry_category_mode = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_mode"));
    entry_category_station = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_station"));
    entry_category_transmitter = GTK_WIDGET(gtk_builder_get_object(builder, "entry_category_transmitter"));
    entry_claimed_score = GTK_WIDGET(gtk_builder_get_object(builder, "entry_claimed_score"));
    entry_operators = GTK_WIDGET(gtk_builder_get_object(builder, "entry_operators"));
    entry_club = GTK_WIDGET(gtk_builder_get_object(builder, "entry_club"));
    entry_name = GTK_WIDGET(gtk_builder_get_object(builder, "entry_name"));
    entry_address = GTK_WIDGET(gtk_builder_get_object(builder, "entry_address"));
    entry_address1 = GTK_WIDGET(gtk_builder_get_object(builder, "entry_address1"));
    entry_address_city = GTK_WIDGET(gtk_builder_get_object(builder, "entry_address_city"));
    entry_address_state_province = GTK_WIDGET(gtk_builder_get_object(builder, "entry_address_state_province"));
    entry_address_postalcode = GTK_WIDGET(gtk_builder_get_object(builder, "entry_address_postalcode"));
    entry_address_country = GTK_WIDGET(gtk_builder_get_object(builder, "entry_address_country"));
    entry_grid_locator = GTK_WIDGET(gtk_builder_get_object(builder, "entry_grid_locator"));
    entry_email = GTK_WIDGET(gtk_builder_get_object(builder, "entry_email"));
    entry_created_by = GTK_WIDGET(gtk_builder_get_object(builder, "entry_created_by"));
    entry_dbname = GTK_WIDGET(gtk_builder_get_object(builder, "entry_dbname"));
    entry_gps_latitude = GTK_WIDGET(gtk_builder_get_object(builder, "entry_gps_latitude"));
    entry_gps_longitude = GTK_WIDGET(gtk_builder_get_object(builder, "entry_gps_longitude"));
    
    // Load configuration to UI
    loadConfigToUI();
    
    // Connect signals
    gtk_builder_connect_signals(builder, this);
    
    g_object_unref(builder);
    
    // Show dialog and wait for response
    gint response = gtk_dialog_run(GTK_DIALOG(dialog));
    
    if (response == GTK_RESPONSE_OK) {
        // Save configuration from UI
        saveConfigFromUI();
        return true;
    }
    
    return false;
}

void CabrilloConfigDialog::loadConfigToUI() {
    if (!config) return;
    
    // Load configuration values to entry widgets
    if (entry_callsign) {
        gtk_entry_set_text(GTK_ENTRY(entry_callsign), 
            config->get("CabrilloConfig.CALLSIGN", "").c_str());
    }
    
    if (entry_contest) {
        gtk_entry_set_text(GTK_ENTRY(entry_contest), 
            config->get("CabrilloConfig.CONTEST", "").c_str());
    }
    
    if (entry_location) {
        gtk_entry_set_text(GTK_ENTRY(entry_location), 
            config->get("CabrilloConfig.LOCATION", "").c_str());
    }
    
    if (entry_start_of_log) {
        gtk_entry_set_text(GTK_ENTRY(entry_start_of_log), 
            config->get("CabrilloConfig.START-OF-LOG", "").c_str());
    }
    
    if (entry_category_operator) {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(entry_category_operator), 
            config->get("CabrilloConfig.CATEGORY-OPERATOR", "").c_str());
    }
    
    if (entry_category_band) {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(entry_category_band), 
            config->get("CabrilloConfig.CATEGORY-BAND", "").c_str());
    }
    
    if (entry_category_power) {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(entry_category_power), 
            config->get("CabrilloConfig.CATEGORY-POWER", "").c_str());
    }
    
    if (entry_category_assisted) {
        gtk_entry_set_text(GTK_ENTRY(entry_category_assisted), 
            config->get("CabrilloConfig.CATEGORY-ASSISTED", "").c_str());
    }
    
    if (entry_category_mode) {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(entry_category_mode), 
            config->get("CabrilloConfig.CATEGORY-MODE", "").c_str());
    }
    
    if (entry_category_station) {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(entry_category_station), 
            config->get("CabrilloConfig.CATEGORY-STATION", "").c_str());
    }
    
    if (entry_category_transmitter) {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(entry_category_transmitter), 
            config->get("CabrilloConfig.CATEGORY-TRANSMITTER", "").c_str());
    }
    
    if (entry_claimed_score) {
        gtk_entry_set_text(GTK_ENTRY(entry_claimed_score), 
            config->get("CabrilloConfig.CLAIMED-SCORE", "").c_str());
    }
    
    if (entry_operators) {
        gtk_entry_set_text(GTK_ENTRY(entry_operators), 
            config->get("CabrilloConfig.OPERATORS", "").c_str());
    }
    
    if (entry_club) {
        gtk_entry_set_text(GTK_ENTRY(entry_club), 
            config->get("CabrilloConfig.CLUB", "").c_str());
    }
    
    if (entry_name) {
        gtk_entry_set_text(GTK_ENTRY(entry_name), 
            config->get("CabrilloConfig.NAME", "").c_str());
    }
    
    if (entry_address) {
        gtk_entry_set_text(GTK_ENTRY(entry_address), 
            config->get("CabrilloConfig.ADDRESS", "").c_str());
    }
    
    if (entry_address1) {
        gtk_entry_set_text(GTK_ENTRY(entry_address1), 
            config->get("CabrilloConfig.ADDRESS1", "").c_str());
    }
    
    if (entry_address_city) {
        gtk_entry_set_text(GTK_ENTRY(entry_address_city), 
            config->get("CabrilloConfig.ADDRESS-CITY", "").c_str());
    }
    
    if (entry_address_state_province) {
        gtk_entry_set_text(GTK_ENTRY(entry_address_state_province), 
            config->get("CabrilloConfig.ADDRESS-STATE-PROVINCE", "").c_str());
    }
    
    if (entry_address_postalcode) {
        gtk_entry_set_text(GTK_ENTRY(entry_address_postalcode), 
            config->get("CabrilloConfig.ADDRESS-POSTALCODE", "").c_str());
    }
    
    if (entry_address_country) {
        gtk_entry_set_text(GTK_ENTRY(entry_address_country), 
            config->get("CabrilloConfig.ADDRESS-COUNTRY", "").c_str());
    }
    
    if (entry_grid_locator) {
        gtk_entry_set_text(GTK_ENTRY(entry_grid_locator), 
            config->get("CabrilloConfig.GRID-LOCATOR", "").c_str());
    }
    
    if (entry_email) {
        gtk_entry_set_text(GTK_ENTRY(entry_email), 
            config->get("CabrilloConfig.EMAIL", "").c_str());
    }
    
    if (entry_created_by) {
        gtk_entry_set_text(GTK_ENTRY(entry_created_by), 
            config->get("CabrilloConfig.CREATED-BY", "").c_str());
    }
    
    if (entry_dbname) {
        gtk_entry_set_text(GTK_ENTRY(entry_dbname), 
            config->get("CabrilloConfig.DBNAME", "").c_str());
    }

    if (entry_gps_latitude) {
        gtk_entry_set_text(GTK_ENTRY(entry_gps_latitude), 
            config->get("CabrilloConfig.GPS-LATITUDE", "").c_str());
    }
    
    if (entry_gps_longitude) {
        gtk_entry_set_text(GTK_ENTRY(entry_gps_longitude), 
            config->get("CabrilloConfig.GPS-LONGITUDE", "").c_str());
    }
}

void CabrilloConfigDialog::saveConfigFromUI() {
    if (!config) return;
    
    // Save configuration values from entry widgets to memory
    if (entry_callsign) {
        config->set("CabrilloConfig.CALLSIGN", gtk_entry_get_text(GTK_ENTRY(entry_callsign)));
    }
    
    if (entry_contest) {
        config->set("CabrilloConfig.CONTEST", gtk_entry_get_text(GTK_ENTRY(entry_contest)));
    }
    
    if (entry_location) {
        config->set("CabrilloConfig.LOCATION", gtk_entry_get_text(GTK_ENTRY(entry_location)));
    }
    
    if (entry_start_of_log) {
        config->set("CabrilloConfig.START-OF-LOG", gtk_entry_get_text(GTK_ENTRY(entry_start_of_log)));
    }
    
    if (entry_category_operator) {
        const gchar* category_operator_text = gtk_combo_box_get_active_id(GTK_COMBO_BOX(entry_category_operator));
        if (category_operator_text) {
            config->set("CabrilloConfig.CATEGORY-OPERATOR", category_operator_text);
        }
    }
    
    if (entry_category_band) {
        const gchar* category_band_text = gtk_combo_box_get_active_id(GTK_COMBO_BOX(entry_category_band));
        if (category_band_text) {
            config->set("CabrilloConfig.CATEGORY-BAND", category_band_text);
        }
    }
    
    if (entry_category_power) {
        const gchar* category_power_text = gtk_combo_box_get_active_id(GTK_COMBO_BOX(entry_category_power));
        if (category_power_text) {
            config->set("CabrilloConfig.CATEGORY-POWER", category_power_text);
        }
    }
    
    if (entry_category_assisted) {
        config->set("CabrilloConfig.CATEGORY-ASSISTED", gtk_entry_get_text(GTK_ENTRY(entry_category_assisted)));
    }
    
    if (entry_category_mode) {
        const gchar* category_mode_text = gtk_combo_box_get_active_id(GTK_COMBO_BOX(entry_category_mode));
        if (category_mode_text) {
            config->set("CabrilloConfig.CATEGORY-MODE", category_mode_text);
        }
    }
    
    if (entry_category_station) {
        const gchar* category_station_text = gtk_combo_box_get_active_id(GTK_COMBO_BOX(entry_category_station));
        if (category_station_text) {
            config->set("CabrilloConfig.CATEGORY-STATION", category_station_text);
        }
    }
    
    if (entry_category_transmitter) {
        const gchar* category_transmitter_text = gtk_combo_box_get_active_id(GTK_COMBO_BOX(entry_category_transmitter));
        if (category_transmitter_text) {
            config->set("CabrilloConfig.CATEGORY-TRANSMITTER", category_transmitter_text);
        }
    }
    
    if (entry_claimed_score) {
        config->set("CabrilloConfig.CLAIMED-SCORE", gtk_entry_get_text(GTK_ENTRY(entry_claimed_score)));
    }
    
    if (entry_operators) {
        config->set("CabrilloConfig.OPERATORS", gtk_entry_get_text(GTK_ENTRY(entry_operators)));
    }
    
    if (entry_club) {
        config->set("CabrilloConfig.CLUB", gtk_entry_get_text(GTK_ENTRY(entry_club)));
    }
    
    if (entry_name) {
        config->set("CabrilloConfig.NAME", gtk_entry_get_text(GTK_ENTRY(entry_name)));
    }
    
    if (entry_address) {
        config->set("CabrilloConfig.ADDRESS", gtk_entry_get_text(GTK_ENTRY(entry_address)));
    }
    
    if (entry_address1) {
        config->set("CabrilloConfig.ADDRESS1", gtk_entry_get_text(GTK_ENTRY(entry_address1)));
    }
    
    if (entry_address_city) {
        config->set("CabrilloConfig.ADDRESS-CITY", gtk_entry_get_text(GTK_ENTRY(entry_address_city)));
    }
    
    if (entry_address_state_province) {
        config->set("CabrilloConfig.ADDRESS-STATE-PROVINCE", gtk_entry_get_text(GTK_ENTRY(entry_address_state_province)));
    }
    
    if (entry_address_postalcode) {
        config->set("CabrilloConfig.ADDRESS-POSTALCODE", gtk_entry_get_text(GTK_ENTRY(entry_address_postalcode)));
    }
    
    if (entry_address_country) {
        config->set("CabrilloConfig.ADDRESS-COUNTRY", gtk_entry_get_text(GTK_ENTRY(entry_address_country)));
    }
    
    if (entry_grid_locator) {
        config->set("CabrilloConfig.GRID-LOCATOR", gtk_entry_get_text(GTK_ENTRY(entry_grid_locator)));
    }
    
    if (entry_email) {
        config->set("CabrilloConfig.EMAIL", gtk_entry_get_text(GTK_ENTRY(entry_email)));
    }
    
    if (entry_created_by) {
        config->set("CabrilloConfig.CREATED-BY", gtk_entry_get_text(GTK_ENTRY(entry_created_by)));
    }
    
    if (entry_dbname) {
        config->set("CabrilloConfig.DBNAME", gtk_entry_get_text(GTK_ENTRY(entry_dbname)));
    }
    if (entry_gps_latitude) {
        config->set("CabrilloConfig.GPS-LATITUDE", gtk_entry_get_text(GTK_ENTRY(entry_gps_latitude)));
    }
    
    if (entry_gps_longitude) {
        config->set("CabrilloConfig.GPS-LONGITUDE", gtk_entry_get_text(GTK_ENTRY(entry_gps_longitude)));
    }
    
    // Save configuration to file
    if (config->saveConfig("config/cabrilloConfig.ini")) {
        std::cout << "Cabrillo配置已保存到文件和内存中" << std::endl;
    } else {
        std::cerr << "保存Cabrillo配置到文件失败" << std::endl;
    }
}

// C wrapper function for GTK callback
extern "C" void on_cw_cabrillo_button_clicked(GtkWidget* widget, gpointer data) {
    std::cout << "Cabrillo配置按钮被点击" << std::endl;
    
    // 通过UIManager获取主窗口
    GtkBuilder* builder = UIManager::get_builder();
    if (!builder) {
        std::cerr << "无法获取UI构建器" << std::endl;
        return;
    }
    
    GtkWidget* main_window = UIManager::get_main_window(builder);
    if (!main_window) {
        std::cerr << "无法获取主窗口" << std::endl;
        return;
    }
    
    // 创建并显示Cabrillo配置对话框
    CabrilloConfigDialog dialog;
    dialog.show(GTK_WINDOW(main_window));
}